package com.mic.yzzn.plc.common.instructions.response;

import org.apache.commons.lang.StringUtils;

import com.mic.yzzn.plc.common.instructions.basic.PLCResponseInstruction;

/**
 * Created by ASH on 16/3/7.
 */
public class PLCSingleWriteResponseInstruction extends PLCResponseInstruction
{
	/**
	 * 初始化方法
	 */
    public PLCSingleWriteResponseInstruction()
    {
        new PLCResponseInstruction();

        instructionTypeCode = PLCResponseInstruction.PLCInstructionTypeResponseCode;
        responseCode = PLCResponseInstruction.WRITE_RESPONSE_CODE;
    }

    /**
     * 用字符串初始化
     */
    @Override
    public String initWithString(String instructionString) throws IllegalArgumentException
    {
    	String tempResult = super.initWithString(instructionString);

    	// 检测是否已经出错
    	if (tempResult.length() == 0)
		{
			return tempResult;
		}
    	
    	int responseCodeLength = responseCode.length();
    	if (!tempResult.substring(0, responseCodeLength).equals(responseCode))
		{
			throw new IllegalArgumentException("PLCSingleReadResponseInstruction's response code must be \"" + responseCode + "\"");
		}
    	// 截断响应码
    	tempResult = StringUtils.substringAfter(tempResult, responseCode);
    	
    	return tempResult;
    }
    
}
